package cheesymock;

import java.lang.reflect.*;
import static cheesymock.MethodUtil.*;

/**
 * Validator that searches the delegate object for methods annotated
 * with {@link Check}, and calls all of them in turn.
 *
 * @author fredrik
 */
public class CheckDelegate extends Validator {

	@Override
	public void check() {
		if (delegate != null) {
			try {
				for(Method m: delegate.getClass().getDeclaredMethods()) {
					if (m.getAnnotation(Check.class) != null) {
						Object[] p = new Object[m.getParameterTypes().length];
						m.invoke(delegate, p);
					}
				}
			}
			catch (Exception e) {
				throw toRuntimeException(e);
			}
		}
	}
}